describe('See transaction history controller', function(){
	
	var controller;
	
	beforeEach(function(){
		var t_appCtrl = App.ApplicationController.create();
		var t_alertCtrl = App.AlertController.create();
		var t_loadingCtrl = App.LoadingController.create();
        var calendarCtrl = App.CalendarController.create();
        var t_navbarCtrl = App.NavbarController.create();
		App.SeeTransactionHistoryController.reopen({
			
			init:function(){
                this.set('controllers', Em.Object.create({
                    application: t_appCtrl,
                    alert: t_alertCtrl,
                    loading: t_loadingCtrl,
                    calendar: calendarCtrl,
                    navbar : t_navbarCtrl
                }));
			}

		});
	});
	
    describe('prepareController', function() {
        beforeEach(function(){
            controller = App.SeeTransactionHistoryController.create();
            spyOn(App.api_accountHistory, 'getAccountHistory').andCallFake(function(account, callback) {
                callback([], {});
            });
            spyOn(controller, 'accountHistoryCallback');
            spyOn(controller, 'showLoading');
        });

        it('should get account history data and display list', function() {
            var account = App.HaveAccount.create();
            spyOn(App.Router, 'getParameter').andReturn(account);
            spyOn(controller, 'selectNavItem');
            controller.prepareController();
            expect(controller.get('navItems')).not.toBeNull();
            expect(controller.get('account')).toBe(account);
            expect(controller.selectNavItem).toHaveBeenCalledWith('list');
            expect(controller.showLoading).toHaveBeenCalled();
            expect(App.api_accountHistory.getAccountHistory).toHaveBeenCalledWith(account, jasmine.any(Function));
            expect(controller.accountHistoryCallback).toHaveBeenCalled();
        });

        it('should not refresh when coming back from transactionDetail OR accountDetail', function() {
            var account = App.HaveAccount.create();
            spyOn(App.Router, 'getParameter').andReturn(null);
            spyOn(App.RouteHistory, 'previousRoute').andReturn('see.transactionDetail');
            spyOn(controller, 'selectNavItem');
            spyOn(window, 'setTimeout').andCallFake(function(callback){
                callback();
            });
            controller.prepareController();
            expect(controller.get('account')).toBe(null);
            expect(controller.selectNavItem).toHaveBeenCalledWith('list');
            expect(controller.get('showListView')).toBe(true);
        });

        it('should show loading when coming back from transactionDetail OR accountDetail', function() {
            var account = App.HaveAccount.create();
            spyOn(App.Router, 'getParameter').andReturn(null);
            spyOn(App.RouteHistory, 'previousRoute').andReturn('see.transactionDetail');
            spyOn(controller, 'selectNavItem');
            spyOn(window, 'setTimeout').andCallFake(function(callback){
                callback();
            });
            controller.set('showLoadingForLongRendering', true);
            controller.prepareController();
            expect(controller.get('account')).toBe(null);
            expect(controller.showLoading).toHaveBeenCalled();
            expect(controller.get('showListView')).toBe(true);
        });
    });

    describe('selectNavItem', function() {
        beforeEach(function(){
            controller = App.SeeTransactionHistoryController.create();
            controller.set('navItems',Ember.A([
                App.SeeTransactionsNavItem.create({
                    id: 'list',
                    title: '',
                    isActive: false,
                    isList: true
                }),
                App.SeeTransactionsNavItem.create({
                    id: 'calendar',
                    title: '',
                    isActive: false,
                    isList:false
                })
            ]));
        });

        it('should show list view', function() {
            var item_id = 'list';
            controller.selectNavItem(item_id);

            var items = controller.get('navItems');
            for(var i=0; i<items.length; i++){
                expect(items[i].get('isActive')).toBe(item_id === items[i].id);
            }

            expect(controller.get('hideList')).toBe(false);
            expect(controller.get('showCalendarView')).toBe(false);
        });

        it('should show calendar view', function(){
            spyOn(controller.get('controllers.calendar'), 'setCalendarData');
            controller.selectNavItem('calendar');
            expect(controller.get('hideList')).toBe(true);
            expect(controller.get('showCalendarView')).toBe(true);

            var calendarData = {
                _target:controller,
                _flaggedDates:controller.get('calendarFlaggedDatesArray'),
                _currentTimestamp:App.getSession().getTimeStamp()
            };

            expect(controller.get('controllers.calendar').setCalendarData).toHaveBeenCalledWith(calendarData);
        });
    });

    describe("accountHistoryCallback", function(){

        it("should invoke the paymentHistory", function(){

            var account = App.BaseAccount.create();
            account.requiresPaymentHistory = true;

            spyOn(controller, 'set');
            spyOn(controller, 'paymentHistoryCallback');
            spyOn(controller, 'hasError').andReturn(false);
            spyOn(controller, 'get').andReturn(account);
            spyOn(App.api_paymentHistory, 'getScheduledPaymentHistory').andCallFake(function(account,callback){
                callback();
            });

            controller.accountHistoryCallback({});

            expect(controller.hasError).toHaveBeenCalled();
            expect(App.api_paymentHistory.getScheduledPaymentHistory).toHaveBeenCalled();
            expect(controller.paymentHistoryCallback).toHaveBeenCalled();
        });
    });

});